package UDPSocket;

import java.io.IOException;
import java.net.*;

/**
 * @author 王艺锡
 * @version 1.0
 */
public class UDPSendB {
    public static void main(String[] args) throws IOException {
        //1.创建一个DatagrameSocket对象，准备在9998端口接收数据
        DatagramSocket socket = new DatagramSocket(9998);

        //2.创建一个DatagramPacket对象，将需要发送的消息封装到packet中
        byte[] bytes = "hello,好久不见".getBytes();
        DatagramPacket packet =
                new DatagramPacket(bytes, bytes.length, InetAddress.getByName("192.168.1.7"), 9999);

        //3.调用发送方法
        socket.send(packet);


        //接收
        byte bytes1[] =new byte[1024];
        packet = new DatagramPacket(bytes1, bytes1.length);
        socket.receive(packet);

        //拆包
        int length = packet.getLength();
        byte[] data = packet.getData();
        String s = new String(data, 0, length);
        System.out.println(s);

        //4.关闭资源
        socket.close();
        System.out.println("B端退出");

    }
}
